package com.it.base.core.system.dict.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.it.base.common.view.PageResult;
import com.it.base.common.view.Result;
import com.it.base.common.view.ResultCode;
import com.it.base.core.system.dict.model.covert.SysDictTypeCovert;
import com.it.base.core.system.dict.model.dto.SysDictTypeDTO;
import com.it.base.core.system.dict.model.dto.SysDictTypeListDTO;
import com.it.base.core.system.dict.model.entity.SysDictType;
import com.it.base.core.system.dict.model.from.SysDictTypeDeleteOneFormDTO;
import com.it.base.core.system.dict.model.from.SysDictTypeSaveFormDTO;
import com.it.base.core.system.dict.model.from.SysDictTypeSelectOneFormDTO;
import com.it.base.core.system.dict.model.from.SysDictTypeSelectPageListFormDTO;
import com.it.base.core.system.dict.model.from.SysDictTypeUpdateFormDTO;
import com.it.base.core.system.dict.model.from.SysDictTypeValidatorFormDTO;
import com.it.base.core.system.dict.service.ISysDictTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 字典
 * 
 * @author wdh
 *
 */
@Slf4j
@RestController
@RequestMapping("system/dict/type")
@ApiSort(value = 1)
@Api(tags = "1.字典类型")
public class SysDictTypeController {

	@Autowired
	private ISysDictTypeService sysDictTypeService;

	@GetMapping("test")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "1.测试", notes = "测试")
	public Result<Object> test() {
		return Result.SUCCESS();
	}

	@PostMapping("selectPageList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "2.分页查询字典类型", notes = "分页查询字典")
	public Result<PageResult<SysDictTypeListDTO>> selectPageList(
			@Validated @RequestBody SysDictTypeSelectPageListFormDTO sysDictTypeSelectListFormDTO) {
		PageResult<SysDictType> page = sysDictTypeService.selectPageList(sysDictTypeSelectListFormDTO);
		List<SysDictTypeListDTO> datas = SysDictTypeCovert.INSTANCE.entity2DTOList(page.getRows());
		return Result.SUCCESS(new PageResult<SysDictTypeListDTO>(page.getTotal(), datas));
	}

	@PostMapping("validatorExist")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "3.校验字典类型", notes = "校验字典")
	public Result<Boolean> validatorExist(
			@Validated @RequestBody SysDictTypeValidatorFormDTO sysDictTypeValidatorFormDTO) {
		return Result.SUCCESS(this.sysDictTypeService.validatorExist(sysDictTypeValidatorFormDTO));
	}

	@PostMapping("saveOne")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "4.新增字典类型", notes = "新增字典")
	public Result<String> saveOne(@Validated @RequestBody SysDictTypeSaveFormDTO sysDictTypeSaveFormDTO) {
		// 1.校验
		SysDictTypeValidatorFormDTO sysDictTypeValidatorFormDTO = SysDictTypeValidatorFormDTO.builder()//
				.type(sysDictTypeSaveFormDTO.getType())//
				.build();
		sysDictTypeValidatorFormDTO.setId(null);
		boolean flag = this.sysDictTypeService.validatorExist(sysDictTypeValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100116);
		}

		// 2.保存
		SysDictType dict = SysDictTypeCovert.INSTANCE.from2Entity(sysDictTypeSaveFormDTO);
		this.sysDictTypeService.save(dict);
		return Result.SUCCESS(dict.getId());
	}

	@PostMapping("selectOne")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "5.查询字典类型", notes = "查询字典")
	public Result<SysDictTypeDTO> selectOne(
			@Validated @RequestBody SysDictTypeSelectOneFormDTO sysDictTypeSelectOneFormDTO) {
		SysDictType sysDictType = this.sysDictTypeService.selectOne(sysDictTypeSelectOneFormDTO.getId());
		SysDictTypeDTO sysDictTypeDTO = SysDictTypeCovert.INSTANCE.entity2DTO(sysDictType);
		return Result.SUCCESS(sysDictTypeDTO);
	}

	@PutMapping("updateOne")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "6.更新字典类型", notes = "更新字典")
	public Result<?> updateOne(@Validated @RequestBody SysDictTypeUpdateFormDTO sysDictTypeUpdateFormDTO) {
		// 1.校验
		SysDictTypeValidatorFormDTO sysDictTypeValidatorFormDTO = SysDictTypeValidatorFormDTO.builder()//
				.type(sysDictTypeUpdateFormDTO.getType())//
				.id(sysDictTypeUpdateFormDTO.getId())//
				.build();
		boolean flag = this.sysDictTypeService.validatorExist(sysDictTypeValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100116);
		}

		SysDictType sysDictType = SysDictTypeCovert.INSTANCE.from2Entity(sysDictTypeUpdateFormDTO);
		this.sysDictTypeService.updateById(sysDictType);
		return Result.SUCCESS(sysDictType.getId());
	}

	@DeleteMapping("deleteOne")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "7.删除字典类型", notes = "删除字典")
	public Result<Object> deleteOne(@Validated @RequestBody SysDictTypeDeleteOneFormDTO sysDictTypeDeleteOneFormDTO) {
		this.sysDictTypeService.removeById(sysDictTypeDeleteOneFormDTO.getId());
		return Result.SUCCESS();
	}

}
